var fs = require('fs');
var iconvlite = require('iconv-lite');

function readFileSync_encoding(filename) {
    var content = fs.readFileSync(filename);
    return iconvlite.decode(content, 'gbk');
}

var c = fs.readFileSync('./bb', 'UTF-8');
var lines = c.split('\n');


var book = {
	"name": "圣经新约",
	"chapters": []
};

lines.forEach(function(l){
	var title = l.trim();

	var tmp = {
		title: title,
		contents: []
	};
	book.chapters.push(tmp);


	var dirs = fs.readdirSync('./data/新约');
	dirs.forEach(function(dir){
		if(dir.indexOf(title) >= 0){

			// var chps = fs.readdirSync('./data/新约/' + dir);

			// chps.forEach(function(ch){
			// 	console.log(ch, ch.length)
			// 	if(ch.length == 7){
			// 		var newch = ch.replace('第', '第0');
			// 		var result = fs.renameSync('./data/新约/' + dir + '/' + ch, './data/新约/' + dir + '/' + newch);
			// 		console.log(result)
			// 	}
			// });


			var chps = fs.readdirSync('./data/新约/' + dir);
			chps.forEach(function(ch){
				var content = readFileSync_encoding('./data/新约/' + dir + '/' + ch);
				var bbb = [];

				content.split('\r\n').forEach(function(lineContent){
					lineContent = lineContent.trim();
					var i = lineContent.indexOf('　');
					if(i > 0){
						lineContent = lineContent.substr(i + 1);
						bbb.push(lineContent);
					}
				});

				tmp.contents.push({
					content: bbb
				});
			});

		}
	});


});

// console.log(book)
console.log(JSON.stringify(book));